package com.yanze.service.impl;

import com.yanze.mapper.FriendcommitMapper;
import com.yanze.pojo.Friendcircle;
import com.yanze.pojo.Friendcommit;
import com.yanze.service.CommitFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommitFriendimpl implements CommitFriendService {

    @Autowired
    FriendcommitMapper mCommitMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addFriendCommit(Friendcommit commit) {
        int flag = mCommitMapper.insert(commit);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Friendcommit> getDynamicCommit(String dynamicId) {
        Example example = new Example(Friendcommit.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmptyOrWhitespace(dynamicId)) {
            criteria.andLike("findcircleid", "%" + dynamicId + "%");
        }
        example.orderBy("time").desc();
        List<Friendcommit> commits = mCommitMapper.selectByExample(example);
        return commits;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteCommit(Long commitId) {
        int flag = mCommitMapper.deleteByPrimaryKey(commitId);
        return flag;
    }
}
